import request from "@/uitls/request";
import {
  SpuResponseData,
  AllTradeMark,
  HasSaleAttrResponseData,
  SpuHasImg,
  spuSaleAttResponseData,
  SkuInfoData,
} from "./type";
enum API {
  LIST_URL = "/admin/product/",
  UPLOAD_URL = "/admin/product/fileUpload/",
  //获取全部品牌的数据
  ALLTRADEMARK_URL = "/admin/product/baseTrademark/getTrademarkList",
  //获取某个SPU下的全部的售卖商品的图片数据
  IMAGE_URL = "/admin/product/spuImageList/",
  //获取某一个SPU下全部的已有的销售属性接口地址
  SPUHASSALEATTR_URL = "/admin/product/spuSaleAttrList/",
  //获取整个项目全部的销售属性[颜色、版本、尺码]
  ALLSALEATTR_URL = "/admin/product/baseSaleAttrList",
  //追加一个新的SPU
  ADDSPU_URL = "/admin/product/saveSpuInfo",
  //更新已有的SPU
  UPDATESPU_URL = "/admin/product/updateSpuInfo",
  //追加一个新增的SKU地址
  ADDSKU_URL = "/admin/product/saveSkuInfo",
  //查看某一个已有的SPU下全部售卖的商品
  SKUINFO_URL = "/admin/product/findBySpuId/",
  //删除已有的SPU
  REMOVESPU_URL = "/admin/product/deleteSpu/",
}
export const reqList = (
  page: number,
  limit: number,
  category3Id: number | string
) =>
  request.get<any, SpuResponseData>(
    API.LIST_URL + `${page}/${limit}?category3Id=${category3Id}`
  );
// 图片上传
export const reqUploadImgs = (file: File) =>
  request.post<any, any>(API.UPLOAD_URL, file);

//获取全部的SPU的品牌的数据
export const reqAllTradeMark = () =>
  request.get<any, AllTradeMark>(API.ALLTRADEMARK_URL);

//获取某一个已有的SPU下全部商品的图片地址
export const reqSpuImageList = (spuId: number) =>
  request.get<any, SpuHasImg>(API.IMAGE_URL + spuId);

//获取某一个已有的SPU拥有多少个销售属性
export const reqSpuHasSaleAttr = (spuId: number) =>
  request.get<any, spuSaleAttResponseData>(API.SPUHASSALEATTR_URL + spuId);

//获取全部的销售属性
export const reqAllSaleAttr = () =>
  request.get<any, HasSaleAttrResponseData>(API.ALLSALEATTR_URL);
//添加一个新的SPU的
//更新已有的SPU接口
//data:即为新增的SPU|或者已有的SPU对象
export const reqAddOrUpdateSpu = (data: any) => {
  //如果SPU对象拥有ID,更新已有的SPU
  if (data.id) {
    return request.post<any, any>(API.UPDATESPU_URL, data);
  } else {
    return request.post<any, any>(API.ADDSPU_URL, data);
  }
};
//添加SKU的请求方法
export const reqAddSku = (data: any) =>
  request.post<any, any>(API.ADDSKU_URL, data);

//获取SKU数据
export const reqSkuList = (spuId: number | string) =>
  request.get<any, SkuInfoData>(API.SKUINFO_URL + spuId);

//删除已有的SPU
export const reqRemoveSpu = (spuId: number | string) =>
  request.delete<any, any>(API.REMOVESPU_URL + spuId);
